// ===================================================================== Declare
const { $http, $fn, $Lazy } = window
// ===================================================================== Lazy
const Confirm = $Lazy.file(()=>import('@antd/common/confirm-submit'))
// ===================================================================== 
const Index = function(name, { selectedKeys, selectedRows, query, data, btnLen } ){
	const { result_model, check_view_mode } = query		// 添加spec_code spec_type check_view_mode commit_type参数 2021-9-9  --马婷 
	// 组合提交参数
	const len = selectedKeys.length
	const select = len > 0 ? selectedRows : data
	let forms = select.map(v => {
		const result = { 
			uuid: v.uuid, 
			spec_abnormal:v.spec_abnormal, 
			content:v.content || '', 
			dilution_multiple:v.dilution_multiple, 
			result:v.result, 
			result1:v.result1,
			check_view_mode:check_view_mode,
			spec_type:v.spec_type,
			spec_code:v.spec_code,
			parent_kind_id:v.parent_kind_id,
			commit_type: this.rowsType		// 区分是左边表格(值为1)还是右边表格(值为2) 2021-9-9  --马婷
		}
		// 分栏参数
		if (check_view_mode && this.rowsType === 1) {
			result['project_id'] = v.project_id
			result['result_model'] = result_model
			result['status'] = query.status
		}
		return result
	})
	
	const spec_codes = selectedRows.map(v => v.spec_code)
	// 从上级获取参数
	let param = { ids: selectedKeys, ...query, spec_codes, commit_type: this.rowsType }
	if(!this.isBingLiMode){
		param = { ...param, cross_page_submit:this.cross_page_submit||0 }
	}
	// delete param.result_model
	const reasonParam = { operation_source: 1 }
	const _param = { ids:selectedKeys, result_model }
	// 审核批准参数
	const auditParam = {...param, type:1 }
	
	const status = +param.status
	
	let saveTxt = `此次将操作【${forms.length}】条数据保存结果，是否确认操作？`
	if(status === 45002){
		saveTxt = `保存结果后，数据将退回到结果录入状态，此次将操作【${forms.length}】条数据保存结果，是否确认操作？`
	}else if(status === 45003){
		saveTxt = `保存结果后，数据将退回到已提交状态，此次将操作【${forms.length}】条数据保存结果，是否确认操作？`
	}
	
	const _data = {
		'上机' : { msg:`此次将操作【${len}】条数据上机，是否确认操作？`, api:'ts-report-card/sendtodevice', param },
		// 上机增加空结果上机 2021-9-28  --马婷
		'全部空结果上机' : { msg:`此次操作将全部【${btnLen}】条空结果数据上机，是否确认操作？`, api:'ts-report-card/projectToDeviceByNull', param:{...param,is_null: 1} },
		'选择性空结果上机' : { msg:`此次操作将选中的【${btnLen}】条空结果数据上机，是否确认操作？`, api:'ts-report-card/projectToDeviceByNull', param:{...param,is_null: 2} },
		'保存结果' : { msg:saveTxt, api:'result-unit-item/updateall', refresh:true, param:{forms:JSON.stringify(forms)} },
		'提交' : { msg:`此次将操作【${len}】条数据提交，是否确认操作？`, api:'ts-report-card/uptest', param:{...param, type:1}, clear:true, refresh:true,  },
		'删除结果' : { msg:`此次将操作【${len}】条数据删除结果，是否确认操作？`, api:'ts-report-card/delresult', param, refresh:true, info:true },
		'审核' : { msg:`此次将操作【${len}】条数据审核，是否确认操作？`, api:'ts-report-card/upsub', param:auditParam, clear:true, refresh:true,  },
		// 是否使用上次报告时间 2021-9-2 11:21:41 ?F10: PM? --伍智睿
		'批量批准' : { msg:`此次将批量批准【${len}】条数据，是否确认操作？`, api:'ts-report-card/upaudit',  param:{...auditParam, use_old_approve_at: this.use_old_approve_at}, clear:true, refresh:true,  },
		'手动批准' : { msg:`此次将手动批准【${len}】条数据，是否确认操作？`, api:'ts-report-card/upaudit',  param:{...auditParam, use_old_approve_at: this.use_old_approve_at}, clear:true, refresh:true,  },
		
		'回退' : { msg:`此次将回退【${len}】条数据，是否确认操作？`, api:'ts-report-card/upBack', param, clear:true, refresh:true,  },
		
		'迟发' : { msg:`此次将操作【${len}】条数据迟发，是否确认操作？`, api:'ts-report-card/updelay', param:{...param, ...reasonParam}, modal:true, clear:true, refresh:true,  },
		'退单' : {  msg:`此次将操作【${len}】条数据退单，是否确认操作？`, api:'ts-report-card/upstatusabnormal',param:{...param, status_abnormal:45007},  modal:true, clear:true, refresh:true, },
		'项目终止' : { msg:`此次将操作【${len}】条数据项目终止，是否确认操作？`, api:'ts-report-card/upstatusabnormal',param:{...param, status_abnormal:45008},  modal:true, clear:true, refresh:true,  },
	
		// 44015
		'接种' : { msg:'确认批量接种？', api:'ts-report-card/vaccinate', param:_param, clear:true, refresh:true,   },
		'收获' : { msg:'确认批量收获？', api:'ts-report-card/harvest', param:_param, clear:true, refresh:true,   },
		// 44018
		'前处理' : { msg:'确认批量前处理？', api:'ts-report-card/before', param:_param, clear:true, refresh:true,   },
		'杂交' : { msg:'确认批量杂交？', api:'ts-report-card/hybridization', param:_param, clear:true, refresh:true,   },
		'后处理' : { msg:'确认批后处理？', api:'ts-report-card/after', param:_param, clear:true, refresh:true,   },
	}
	
	Confirm.then(f=>{
		const rows = _data[name]
		let { param } = rows
		if(!$fn.hasObject(rows)){ return $fn.toast(name + '不存在') }
		
		// 删除结果
		if( name === '删除结果' && [45002, 45003].includes(+param.status)){
			rows.info = false
			rows.clear = true
		}
		
		let { api, msg, modal, clear, refresh, info } = rows
		// 已迟发: 提交
		if(name === '提交' && +param.status === 45006){ api = 'ts-report-card/updelaysub'}
		
		if(this.isBingLiMode){
			if(name === '审核') { api ='ts-report-card/upaudit' }
		}
		// let uptest = true
		
		// if([44014].includes(result_model)){
		// 	api = 'result-unit-item/updateall'
		// 	uptest = false
		// 	param = { forms: JSON.stringify(forms)  }
		// }
	
		f(api, { 
			title:name,  
			msg, 
			param,
			onAwait: async () => {
				if(name === '提交' && +param.status === 45001){
					await $http.submit(null, 'result-unit-item/updateall', { param:{ forms: JSON.stringify(forms)  } })
				}
			},
			onOk: async ()=>{
				if(clear){  
					this.clear()
					this.props.personRef().reset()
				}
				if(refresh){ 
					this.onRefresh()
					let { key } = this.state
					if(!key) key = 0
					key++
					this.setState({ key })
				}
				if(info){ this.onRefreshInfo() }
			},
			onMy: modal ? () => this.reasonModal.open(name,rows) : null
		})
	})
}

export default Index